const router = require('koa-router')()
//const { getTodoList,addItem}=require('../dbop/redis')  //解构赋值

const {getTodoList,addItem,deleteItem} =require('../dbop/mysql')
//const todolist=[]



// router.prefix('/api')
router.get('/', async (ctx, next) => {
  // await ctx.render('index', {
  //   title: 'Hello Koa 2!'
  // })
  ctx.body = 'hello koa2'

})

router.post('/testpost', async (ctx, next) => {
  ctx.body = 'koa2 post'
})


router.post('/saveitem', async (ctx, next) => {

  const data= ctx.request.body

  addItem(data)
 // await addItem(data)
  //  const jstr=JSON.stringify(data)
  //  redisClient.set('todoitem',jstr)
  // todolist.push(data)

ctx.body="ok!!!"
  //ctx.body =await getTodoList()  //'koa2 post'
})


router.post('/deleteitem', async (ctx, next) => {

  const {delid}= ctx.request.body

  ctx.body=await deleteItem(delid)
 // await addItem(data)
  //  const jstr=JSON.stringify(data)
  //  redisClient.set('todoitem',jstr)
  // todolist.push(data)

//"ok!!!"
  //ctx.body =await getTodoList()  //'koa2 post'
})



router.post('/clearitems', async (ctx, next) => {

  //const data= ctx.request.body

 // todolist.length=0//'000000'

  //todolist.splice(0,1)
 // ctx.body = todolist.length  //'koa2 post'
})



router.get('/getallitems', async (ctx, next) => {
 ctx.body =await getTodoList()
//testConn()
//ctx.body=[]
})


// router.get('/api/getallitems', async (ctx, next) => {
//   ctx.body = todolist
// })

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

module.exports = router
